<template>
  <div class="login">
    <div class="login-box">
      <!-- 1. 模板迁移：大部分结构可以直接复用 -->
      <!-- 注意：@/assets/... 路径别名能否正常工作，取决于你的 vite.config.ts 配置 -->
      <img src="@/assets/login/666.png" alt="" />
      <div class="login-form" id="login-form">
        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules"
        >
          <div class="login-form-title">
            <img
              src="https://element-plus.org/images/element-plus-logo.svg"
              style="width: 149px; height: 38px"
              alt=""
              class="logoss"
            />
          </div>
          <el-form-item prop="username">
            <!-- el-input 图标用法在 Element Plus 中有变化 -->
            <el-input
              v-model="loginForm.username"
              type="text"
              autocomplete="off"
              placeholder="账号"
            >
              <template #prefix>
                <i class="iconfont icon-user" />
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="密码"
              show-password
              @keyup.enter="handleLogin"
            >
              <template #prefix>
                <i class="iconfont icon-lock" />
              </template>
            </el-input>
          </el-form-item>
          <el-form-item style="width: 100%">
            <!-- .native 修饰符在 Vue 3 中已废弃 -->
            <el-button
              :loading="loading"
              class="login-btn"
              size="medium"
              type="primary"
              style="width: 100%"
              @click.prevent="handleLogin"
            >
              <span v-if="!loading">登录</span>
              <span v-else>登录中...</span>
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
// 2. 脚本迁移：使用 Composition API 重构
import { ref, reactive, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import type { FormInstance, FormRules } from 'element-plus';
import { useUserStore } from '@/store/user';

// 3. 业务逻辑实现

// 获取路由和 Store 实例
const router = useRouter();
const route = useRoute();
const userStore = useUserStore();

// 定义响应式状态
const loading = ref(false);
const loginForm = reactive({
  username: 'admin', // 初始值
  password: '123456',
});
const loginFormRef = ref<FormInstance>();
const redirect = ref<string | undefined>(undefined);

// 监听路由变化，获取 redirect 参数
// onMounted 会在组件挂载后执行一次
onMounted(() => {
  redirect.value = route.query.redirect as string | undefined;
});

// 4. 自定义表单校验规则 (Vue 3 方式)
const validateUsername = (rule: any, value: string, callback: (error?: Error) => void) => {
  if (!value) {
    callback(new Error('请输入用户名'));
  } else {
    callback();
  }
};
const validatePassword = (rule: any, value: string, callback: (error?: Error) => void) => {
  if (value.length < 6) {
    callback(new Error('密码必须在6位以上'));
  } else {
    callback();
  }
};

// 将校验规则绑定到响应式对象
const loginRules = reactive<FormRules>({
  username: [{ validator: validateUsername, trigger: 'blur', required: true }],
  password: [{ validator: validatePassword, trigger: 'blur', required: true }],
});

// 5. 登录处理函数
// login/index.vue -> handleLogin
const handleLogin = async () => {
  const form = loginFormRef.value;
  if (!form) return;

  try {
    await form.validate();
    loading.value = true;
    await userStore.login(loginForm);
    router.push(redirect.value || '/');
  } catch (error) {
    console.error('校验或登录流程失败:', error);
  } finally {
    loading.value = false;
  }
};
</script>

<style lang="scss" scoped>


/* --- 动画关键帧定义 (保持不变) --- */
@keyframes zoom-in-bg {
  from { transform: scale(1); }
  to { transform: scale(1.1); }
}
@keyframes float-in {
  from { opacity: 0; transform: translateY(50px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes fade-in {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

/* --- 主要样式 --- */

.login {
  display: flex;
  justify-content: center; // 恢复水平居中
  align-items: center;
  height: 100vh;
  overflow: hidden;
  position: relative;
  background-color: #859cbe; // 添加一个基础背景色

  &::before {
    content: '';
    position: absolute;
    top: -5%; left: -5%;
    width: 110%; height: 110%;
    background-image: url("@/assets/login/MountainsOnly(Long).png"); // 替换为您的背景图
    background-repeat: no-repeat;
    background-size: cover;
    background-position: center;
    z-index: 1;
    animation: zoom-in-bg 20s ease-in-out infinite alternate;
  }
}

/*
  关键修改 1: 整体放大登录框
*/
.login-box {
  position: relative;
  z-index: 3;
  width: 1100px; // 从 1000px 增大
  max-width: 95%;
  height: 550px; // 从 475px 增大
  border-radius: 16px;
  display: flex;
  backdrop-filter: blur(12px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
  transition: all 0.3s ease;
  animation: float-in 0.8s ease-out forwards;

  .login-image {
    width: 60%;
    height: 100%;
    border-radius: 16px 0 0 16px;
    object-fit: cover;
  }
}

.login-form-wrapper {
  width: 40%;
  display: flex;
  justify-content: center;
  align-items: center;

  .login-form {
    width: 320px; // 显著增大表单宽度
    & > * {
      opacity: 0;
      animation: fade-in 0.5s ease-out forwards;
    }
    // 交错动画延迟
    .login-form-title { animation-delay: 0.4s; }
    .el-form-item:nth-of-type(1) { animation-delay: 0.5s; }
    .el-form-item:nth-of-type(2) { animation-delay: 0.6s; }
    .el-form-item:nth-of-type(3) { animation-delay: 0.7s; }
  }

  .login-form-title {
    margin-bottom: 50px;
    text-align: center; // 确保标题居中
  }

  .el-form-item {
    margin-bottom: 30px;
  }
}

/*
  关键修改 2: 使用 CSS 变量和更高特异性的选择器，放弃 :deep()
*/
.login-form .el-input,
.login-form .el-button {
  // 利用 Element Plus 的 CSS 变量来统一尺寸和样式
  --el-input-height: 50px;
  --el-border-radius-base: 12px;
  --el-font-size-base: 16px;

  height: var(--el-input-height);
  font-size: var(--el-font-size-base);
}

// 进一步定制输入框
.login-form .el-input .el-input__wrapper {
  background-color: rgba(245, 247, 250, 0.8);
  box-shadow: none;
  transition: all 0.3s;

  &:hover {
    background-color: rgba(255, 255, 255, 0.9);
  }
}

// 聚焦状态
.login-form .el-input.is-focus .el-input__wrapper {
  background-color: #fff;
  box-shadow: 0 0 0 1px var(--el-color-primary) inset;
}


.login-btn {
  width: 100%;
  margin-top: 20px;
  font-weight: 500;
  color: #ffffff;
  background-color: #06c0e5;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(6, 192, 229, 0.4);

  &:hover {
    background-color: #05a9c7;
    transform: translateY(-3px);
    box-shadow: 0 8px 20px rgba(6, 192, 229, 0.6);
  }
}

.iconfont {
  font-size: 18px;
  color: #999;
}


/* --- 响应式设计 (优化) --- */
@media (max-width: 1200px) {
  .login-box {
    width: 960px;
    height: 480px;
  }
}

@media (max-width: 992px) {
  .login-box {
    width: 90vw; // 使用 vw 单位更灵活
    height: auto;
    min-height: 450px;
  }
  .login-image {
    width: 50%;
  }
  .login-form-wrapper {
    width: 50%;
  }
}

@media (max-width: 768px) {
  .login {
    padding: 0; // 手机端不需要边距
  }
  .login-box {
    width: 100%;
    height: 100%;
    max-width: 100%;
    border-radius: 0;
    box-shadow: none;
    background-color: #fff; // 手机端使用纯白背景
    backdrop-filter: none;
  }
  .login-image {
    display: none;
  }
  .login-form-wrapper {
    width: 100%;
    padding: 0 15%; // 使用百分比内边距
  }
}


/* --- 可选的粒子效果 (保持不变) --- */
.particles {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  z-index: 2;
  pointer-events: none;
}

#login-form{
  position: relative;
  top: 140px;
  width: 400px;
}
.logoss{
  position: relative;
  left: 30%;
  margin-bottom: 7%;
}
</style>
